package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;

    @RequestMapping("/getUsername")
    public Result getUsername() {
        //当springsecurity完成认证之后,会将用户信息存到框架提供的上下文对象中
        org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user != null) {
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult page = userService.findPage(queryPageBean);
        return page;
    }

    @RequestMapping("/add")
    public Result add(Integer[] roleIds, @RequestBody User user) {
        try {
            userService.add(roleIds, user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
        return new Result(true, MessageConstant.ADD_USER_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            userService.deleteByUserId(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            User user = userService.findById(id);
            return new Result(true, MessageConstant.GET_USER_SUCCESS, user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_USER_FAIL);
        }

    }

    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer id) {
        try {
            List<Integer> roleIds = userService.findRoleIdsByUserId(id);
            return new Result(true, MessageConstant.GET_ROLEID_SUCCESS, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ROLEID_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(Integer[] roleIds, @RequestBody User user) {
        try {
            userService.edit(roleIds, user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
    }

    //根据RBAC权限模型查询当前用户关联的菜单
   /* @RequestMapping("/getMenuByUsername")
    public Result getMenuByUsername(){
        org.springframework.security.core.userdetails.User user =(org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user!=null) {
            String username = user.getUsername();
            List<Map> menu4User=userService.getMenuByUsername(username);
            return new Result(true,MessageConstant.GET_SUCCESS,menu4User);
        }
        return new Result(false,MessageConstant.GET_FAIL);
    }*/
    @RequestMapping("/getMenuByUsername")
    public Result getMenuByUsername() {
        org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user != null) {
            String username = user.getUsername();
            List<Map> menus = userService.getMenuByUsername2(username);
            return new Result(true, MessageConstant.GET_SUCCESS, menus);
        }
        return new Result(false, MessageConstant.GET_FAIL);
    }
}
